import Vue from 'vue'
import localStorageUtil from '@/utils/localStorageUtil'

/** 权限指令**/
const has = Vue.directive('has', {

  inserted: function(el, binding) {
    if (!hasPermission(binding.value)) {
      el.parentNode.removeChild(el)
    }
  }
})

// 判断是否有权限
function hasPermission(value) {
  // 此处store.getters.getMenuBtnList代表vuex中储存的按钮菜单数据
  const btnPermissions = localStorageUtil.fetch('visableFilter')
  let hasPermissions = false
  for (const key in btnPermissions) {
    if (key === value) {
      // do stuff
      hasPermissions = btnPermissions[key]
      break
    }
  }
  return hasPermissions
}

export default { has }
